﻿using FastCloud.Base;
using FastCloud.Base.Desgin;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/userjob")]
    public class UserJobController : BaseController
    {
        [AutoWired]
        private DB_UserJob DB_UserJob;
        [AutoWired]
        private DB_UserJobSetting DB_UserJobSetting;
        [AutoWired]
        private DB_User DB_User;
        [AutoWired]
        private DB_OU DB_OU;

        [Route("changeUserJob")]
        public IActionResult ChangeUserJob()
        {
            return View("~/Views/ORG/UserJob/UserJob_Change.cshtml");
        }

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/UserJob/UserJob_List.cshtml");
        }

        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/ORG/UserJob/UserJob_Add.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string UserGuid = ValueConvert.ConvertToString(GetFormParam("UserGuid"));
            string sql = "UserGuid=@UserGuid";
            List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "UserGuid", ParamValue = UserGuid } };
            List<Frame_UserJob> list = DB_UserJob.SelectPageList("*", sql, "SortNum desc", ParamList);
            foreach (Frame_UserJob item in list)
            {
                switch (item.JobType)
                {
                    case (int)FastCloud.Base.Enum.ORG.JobType.部门兼职:
                        item.JobTypeText = "部门兼职";
                        item.JobContent = string.Format("登陆人可切换部门身份为：{0}", DB_OU.SelectOne(item.UserJobGuid).OUName);
                        break;
                    case (int)FastCloud.Base.Enum.ORG.JobType.人员兼职:
                        item.JobTypeText = "人员兼职";
                        item.JobContent = string.Format("登陆人可切换人员身份为：{0}", DB_User.SelectOne(item.UserJobGuid).DisplayName);
                        break;
                    default:
                        break;
                }
            }
            int count = DB_UserJob.SelectCount(sql);
            Page page = new Page();
            page.Rows = list;
            page.Total = count;
            return Result.OK(page);
        }

        [Route("opSelectList"), HttpPost]
        public IActionResult OpSelectList()
        {
            string UserGuid = ValueConvert.ConvertToString(GetFormParam("UserGuid"));
            string sql = "UserGuid=@UserGuid";
            List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "UserGuid", ParamValue = UserGuid } };
            List<Frame_UserJob> list = DB_UserJob.SelectPageList("*", sql, "SortNum desc", ParamList);
            foreach (Frame_UserJob item in list)
            {
                switch (item.JobType)
                {
                    case (int)FastCloud.Base.Enum.ORG.JobType.部门兼职:
                        item.JobTypeText = "部门兼职";
                        item.JobContent = string.Format("登陆人可切换部门身份为：{0}", DB_OU.SelectOne(item.UserJobGuid).OUName);
                        break;
                    case (int)FastCloud.Base.Enum.ORG.JobType.人员兼职:
                        item.JobTypeText = "人员兼职";
                        item.JobContent = string.Format("登陆人可切换人员身份为：{0}", DB_User.SelectOne(item.UserJobGuid).DisplayName);
                        break;
                    default:
                        break;
                }
            }
            //把自己的当前身份加入进去
            Frame_UserJob SelfJob = new Frame_UserJob();
            SelfJob.RelationGuid = UserGuid;
            SelfJob.JobContent = "正常用户登陆";
            SelfJob.JobTypeText = "非兼职";
            SelfJob.UserGuid = UserGuid;
            SelfJob.UserJobGuid = "UserCommonLogin";
            list.Insert(0, SelfJob);
            List<Frame_UserJob_Setting> SettingList = DB_UserJobSetting.SelectListByUserGuid(UserGuid);
            foreach (Frame_UserJob userJob in list)
            {
                if (SettingList.Where(a => a.DefaultUserJobGuid == userJob.UserJobGuid).Count() > 0)
                {
                    userJob.IsDefault = true;
                }
                else
                {
                    userJob.IsDefault = false;
                }
            }
            int count = DB_UserJob.SelectCount(sql) + 1;
            Page page = new Page();
            page.Rows = list;
            page.Total = count;
            return Result.OK(page);
        }



        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_UserJob Entity)
        {
            DB_UserJob.Insert(Entity);
            return Result.OK();
        }


        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyIDList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            DB_UserJob.DeleteBatch(KeyIDList);
            return Result.OK();
        }
    }
}
